#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'login_form.ui'
#
# Created: Sun Apr 26 12:49:18 2009
#      by: The PyQt User Interface Compiler (pyuic) 3.17.4
#
# WARNING! All changes made in this file will be lost!


from qt import *
from os import access, R_OK
from discovery_form import *
import anydbm


class Login_Form(QDialog):
    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        QDialog.__init__(self,parent,name,modal,fl)

        if not name:
            self.setName("Login_Form")

        self.Login_box = QGroupBox(self,"Login_box")
        self.Login_box.setGeometry(QRect(10,10,590,100))

        self.textLabel2 = QLabel(self.Login_box,"textLabel2")
        self.textLabel2.setGeometry(QRect(20,60,170,20))

        self.textLabel1 = QLabel(self.Login_box,"textLabel1")
        self.textLabel1.setGeometry(QRect(20,30,160,20))

        self.Admin_account_text = QLineEdit(self.Login_box,"Admin_account_text")
        self.Admin_account_text.setGeometry(QRect(200,30,371,21))

        self.Admin_password_text = QLineEdit(self.Login_box,"Admin_password_text")
        self.Admin_password_text.setEchoMode(QLineEdit.Password)
        self.Admin_password_text.setGeometry(QRect(200,60,371,21))

        self.admin_login_button = QPushButton(self,"admin_login_button")
        self.admin_login_button.setGeometry(QRect(490,120,110,24))

        self.cancel_login_button = QPushButton(self,"cancel_login_button")
        self.cancel_login_button.setGeometry(QRect(370,120,110,24))

        self.languageChange()

        self.resize(QSize(609,155).expandedTo(self.minimumSizeHint()))
        self.clearWState(Qt.WState_Polished)

        self.connect(self.cancel_login_button,SIGNAL("clicked()"),self.cancel_login_button_clicked)
        self.connect(self.admin_login_button,SIGNAL("clicked()"),self.admin_login_button_clicked)


    def languageChange(self):
        self.setCaption(self.__tr("Login Discovery"))
        self.Login_box.setTitle(self.__tr("Administrator Login"))
        self.textLabel2.setText(self.__tr("Administrator password"))
        self.textLabel1.setText(self.__tr("Administrator Account"))
        self.cancel_login_button.setText(self.__tr("&Cancel"))
        self.cancel_login_button.setAccel(QKeySequence(self.__tr("Alt+C")))
        self.admin_login_button.setText(self.__tr("&Login"))
        self.admin_login_button.setAccel(QKeySequence(self.__tr("Alt+L")))


    def admin_login_button_clicked(self):
        """
        admin_login_button_clicked(self):
            this checked if the admin account and password matched on
            the database if found login successfully to discovery
        """
        if not access('Admin_Accounts', R_OK) :
            Message = 'ERROR loading the database, Please contact your manager.'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
            return

        admin_db = anydbm.open('Admin_Accounts','r')
        admin_account = str(self.Admin_account_text.text())
        admin_password = str(self.Admin_password_text.text())
        try :
            if admin_db[admin_account] == admin_password:
                start_discovery = Discovey_form(self)
                self.hide()
                start_discovery.show()
                start_discovery.exec_loop()
            else :
                Message = 'Wrong account or password!'
                QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)
        except KeyError:
            Message = 'Wrong account or password!'
            QMessageBox.critical(self,"Warning!",Message,QMessageBox.Ok)

    def cancel_login_button_clicked(self):
        self.hide()

    def Login_Form_destroyed(self,a0):
        print "Login_Form.Login_Form_destroyed(QObject*): Not implemented yet"

    def cancel_login_button_destroyed(self,a0):
        print "Login_Form.cancel_login_button_destroyed(QObject*): Not implemented yet"

    def __tr(self,s,c = None):
        return qApp.translate("Login_Form",s,c)
